package com.coderMan.generateBeanFromTable.util;

import org.apache.commons.lang.StringUtils;

/**
 * Created by ${fanchunshuai} on 2017-6-7.
 *
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2017 58到家-版权所有
 */
public class GenerateTools {
    /**
     * 生成 get / set方法
     * @param varType
     * @param varName
     * @return
     */
    public static String generateGetSetMethod(String varType,String varName){
        StringBuffer buffer = new StringBuffer();
        buffer.append("    public ");
        buffer.append(varType);
        char[] cs = varName.toCharArray();
        cs[0]-=32;
        String upName = String.valueOf(cs);
        buffer.append(" get" + upName +" (){ return "+varName+"; }\n");
        buffer.append("    public void  set");
        buffer.append(upName+"(");
        buffer.append(varType+" "+varName+") { this.");
        buffer.append(varName+" = "+varName+";}\n\n");
        return buffer.toString();
    }

    /**
     * 获取字段类型对应的 封装类型
     * @param varType
     * @return
     */
    public static String getVarType(String varType){
        if(varType.startsWith("int")){
            return  "Integer";
        }else if(varType.startsWith("varchar")){
            return "String";
        }else if(varType.startsWith("bigint")){
            return "Long";
        }
        else if(varType.startsWith("datetime")){
            return  "Date";
        }

        else if(varType.startsWith("decimal")){
            return "Double";
        }

        else if(varType.startsWith("timestamp")){
            return  "Date";
        }

        else if(varType.startsWith("text")){
            return  "String";
        }
        else if(varType.startsWith("tinyint")){
            return  "Short";
        }
        return " null type";
    }

    /**
     * 根据表名生成类名
     * @param tableName
     * @return
     */
    public static String genearateBeanName(String tableName,String prefix,String suffix){
        String s = "";
        if(tableName.contains("_")){
            for (String var : tableName.split("_")){
                char[] cs = var.toCharArray();
                cs[0]-=32;
                String upName = String.valueOf(cs);
                s+=upName;
            }

        }else{
            char[] cs = tableName.toCharArray();
            cs[0] -= 32;
            s = String.valueOf(cs);
        }

        if(StringUtils.isNotBlank(prefix)){
            s = prefix+s;
        }
        if(StringUtils.isNotBlank(suffix)){
            s = s+suffix;
        }
        return s;
    }
}
